<template>
  <div>
    <r-search
      :searchForm="searchForm"
      :searchHandle="searchHandle"
      :searchData="searchData"
    />
    <r-table
      :isPagination="true"
      :isHandle="true"
      :tableData="tableData"
      :tableCols="tableCols"
      :tablePage="tablePage"
      @refresh="init()"
    >
    </r-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      searchData: {
        cabinet_id: "",
      },
      searchForm: [
        {
          type: "input",
          prop: "cabinet_id",
          placeholder: "设备编号",
          width: 230,
        },
      ],
      searchHandle: [
        {
          label: "搜索",
          type: "success",
          icon: "el-icon-search",
          handle: (e) => this.init(),
        },
      ],
      tableData: [],
      tableCols: [
        {
          label: "设备编号",
          prop: "cabinet_id",
          width: 180,
        },
        // {
        //   label: "升级序号",
        //   prop: "id",
        // },
        {
          label: "文件名",
          prop: "file_name",
        },
        {
          label: "文件大小",
          prop: "file_size",
          formatter: (e) =>
            e.file_size >= 1024 * 1024
              ? (e.file_size / 1024 / 1024).toFixed(2) + "G"
              : e.file_size >= 1024
              ? (e.file_size / 1024).toFixed(2) + "M"
              : e.file_size + "k",
        },
        {
          label: "状态",
          prop: "status",
          formatter: (e) =>
            e.status == "SUCCESS"
              ? "已完成"
              : e.status == "ING"
              ? "进行中"
              : "--",
          itemClass: (e) =>
            e.status == "SUCCESS"
              ? "success"
              : e.status == "ING"
              ? "danger"
              : "--",
        },
        {
          type: "progress",
          label: "升级进度",
          prop: "current_package",
          formatter: (e) =>
            e.current_package && e.total_package
              ? (e.current_package / e.total_package).toFixed(2) * 100
              : "--",
        },
        {
          label: "升级时间",
          prop: "create_time",
        },
        {
          label: "更新时间",
          prop: "update_time",
        },
      ],
      tablePage: { limit: 10, page: 1, total: 0 },
    };
  },

  async mounted() {
    this.init();
  },

  methods: {
    async init() {
      let parm = {
        business: {
          cabinet_id: this.searchData.cabinet_id,
        },
        page_no: this.tablePage.page,
        page_size: this.tablePage.limit,
      };
      let res = await this.apis.get_upgrading(parm);
      if (res.code == 0) {
        this.tableData = res.result || []; //接收后端的数据
        this.tablePage.total = res.count || 0; //拿到总数
      } else {
        this.tableData = [];
      }
    },
  },
};
</script>

<style lang="scss"></style>
